package cc.aiee.demo.springboot.controller.moneris;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cc.aiee.core.bean.R;
import cc.aiee.core.bean.ValidGroups;
import cc.aiee.core.kit.KitHttp;
import cc.aiee.core.kit.KitId;
import cc.aiee.demo.springboot.basic.constant.ContentType;
import cc.aiee.demo.springboot.basic.constant.ResultConst;
import cc.aiee.demo.springboot.controller.moneris.bean.MonerisPurchaseQuery;
import cc.aiee.demo.springboot.controller.moneris.bean.MonerisRefundQuery;
import cc.aiee.moneris.api.MonerisPayApi;
import cc.aiee.moneris.core.pojo.MonerisPayResponse;
import cc.aiee.moneris.model.MonerisPayPurchaseModel;
import cc.aiee.moneris.model.MonerisPayRefundModel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/moneris")
@Tag(name = "moneris pay")
@Slf4j
public class MonerisPayController {

	@PostMapping(value = "/purchase", consumes = ContentType.FORM)
	@Operation(summary = "购买")
	public @ResponseBody R<MonerisPayResponse> create(
			@Validated(value = ValidGroups.Add.class) MonerisPurchaseQuery query) {
		MonerisPayApi api = new MonerisPayApi();

		MonerisPayPurchaseModel model2 = query.model(new MonerisPayPurchaseModel());

		model2.setCrypt("7");
		model2.setDescriptor("LiLiangWei/Rent Battery");
		model2.setOrderNo(KitId.timeId());

		R<MonerisPayResponse> result = api.purchase(model2);
		return result;
	}

	@PostMapping(value = "/refund", consumes = ContentType.FORM)
	@Operation(summary = "退款")
	public @ResponseBody R<MonerisPayResponse> refund(@Valid MonerisRefundQuery query) {
		MonerisPayApi api = new MonerisPayApi();

		MonerisPayRefundModel model2 = query.model(new MonerisPayRefundModel());

		model2.setCrypt("7");
		model2.setDescriptor("LiLiangWei/Order Refund");

		R<MonerisPayResponse> result = api.refund(model2);
		return result;
	}

	@PostMapping(value = "/notify")
	@Operation(summary = "回调")
	public @ResponseBody String notify(HttpServletRequest request) {
		log.info("Moneris Pay : 收到一个请求");
		KitHttp.print(request);
		return ResultConst.SUCCESS;
	}
}
